import random
from datetime import datetime, timedelta
from app.core.config import Settings

# 内存存储验证码
captcha_storage = {}

class CaptchaService:
    @staticmethod
    def generate_captcha(email: str) -> str:
        """生成6位数字验证码"""
        captcha = str(random.randint(100000, 999999))
        captcha_storage[email] = {
            'code': captcha,
            'expire_at': datetime.now() + timedelta(minutes=Settings.CAPTCHA_EXPIRE_MINUTES)
        }
        return captcha

    @staticmethod
    def verify_captcha(email: str, captcha: str) -> bool:
        """验证验证码，测试阶段999000作为万能验证码"""
        if captcha == "999000":
            return True
            
        if email not in captcha_storage:
            return False
            
        stored = captcha_storage[email]
        if datetime.now() > stored['expire_at']:
            del captcha_storage[email]  # 过期清理
            return False
            
        if stored['code'] == captcha:
            del captcha_storage[email]  # 验证后销毁
            return True
        return False
